﻿﻿/* **********************************************************************************
 * Copyright (c) 2011 John Hughes
 *
 * j9Script is licenced under the Microsoft Reciprocal License (Ms-RL).
 *
 * Project Website: http://j9Script.codeplex.com/
 * **********************************************************************************/

using System;
using System.Collections.Generic;
using System.Text;
using System.Globalization;

namespace J9Script
{
    [Serializable]
    public struct ScriptPosition
    {
        int _positionInScriptZeroBased;
        int _lineNumberZeroBased;
        int _positionInLineZeroBased;

        public static readonly ScriptPosition Empty;

        [System.ComponentModel.Browsable(false)]
        public bool IsEmpty
        {
            get
            {
                return ((this._positionInScriptZeroBased == 0) && (this._lineNumberZeroBased == 0) && (this._positionInLineZeroBased == 0));
            }
        }

        /// <summary>
        /// Zero based position in script. (1st position = 0)
        /// </summary>
        public int PositionInScript
        {
            get { return _positionInScriptZeroBased; }
        }

        /// <summary>
        /// One based line number. (1st line = 1)
        /// </summary>
        public int LineNumber
        {
            get { return _lineNumberZeroBased + 1; }
        }

        /// <summary>
        /// Zero based position in line. (1st character position = 0)
        /// </summary>
        public int PositionInLine
        {
            get { return _positionInLineZeroBased; }
        }

        public void NewLine()
        {
            _lineNumberZeroBased++;
            _positionInScriptZeroBased++;
            _positionInLineZeroBased = 0;
        }
        public void NextCharacter()
        {
            _positionInScriptZeroBased++;
            _positionInLineZeroBased++;
        }

        public static bool operator ==(ScriptPosition left, ScriptPosition right)
        {
            return (left._positionInScriptZeroBased == right._positionInScriptZeroBased) && (left._lineNumberZeroBased == right._lineNumberZeroBased) && (left._positionInLineZeroBased == right._positionInLineZeroBased);
        }
        public static bool operator !=(ScriptPosition left, ScriptPosition right)
        {
            return !(left == right);
        }
        public override int GetHashCode()
        {
            return (this._positionInScriptZeroBased ^ this._lineNumberZeroBased ^ this._positionInLineZeroBased);
        }
        public override bool Equals(object obj)
        {
            if (!(obj is ScriptPosition))
                return false;

            ScriptPosition pos = (ScriptPosition)obj;
            return (pos == this);
        }
        

        public override string ToString()
        {
            // I commented this out to make the return value simpler to read.
            //return ("{Script Position: " + (this._positionInScriptZeroBased + 1).ToString(CultureInfo.CurrentCulture) + ", Line #: " + (this._lineNumberZeroBased + 1).ToString(CultureInfo.CurrentCulture) + ", Position in line: " + (this._positionInLineZeroBased + 1).ToString(CultureInfo.CurrentCulture) + ", (numbers are base 1)}");
            return ("Location in script: line " + (this._lineNumberZeroBased + 1).ToString(CultureInfo.CurrentCulture) + ", column " + (this._positionInLineZeroBased + 1).ToString(CultureInfo.CurrentCulture) + ".");
        }
    }
}
